package com.job.updateinfojob.service.impl;


import cn.hutool.core.date.DateTime;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.job.updateinfojob.dto.*;
import com.job.updateinfojob.entity.DisksInfoEntity;
import com.job.updateinfojob.entity.NetworkInfoEntity;
import com.job.updateinfojob.entity.OsInfoEntity;
import com.job.updateinfojob.entity.OsRunTimeInfoEntity;
import com.job.updateinfojob.mapper.DisksinfoMapper;
import com.job.updateinfojob.mapper.NetworkinfoMapper;
import com.job.updateinfojob.mapper.OsinfoMapper;
import com.job.updateinfojob.mapper.OsruntimeinfoMapper;
import com.job.updateinfojob.service.IGetInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @Description
 * @Author 1
 * @Date 2024/8/24 下午4:35
 **/
@Service
@Slf4j
public class IGetInfoServiceImpl implements IGetInfoService {

    @Resource
    private OsinfoMapper osinfoMapper;
    @Resource
    private OsruntimeinfoMapper osruntimeinfoMapper;
    @Resource
    private NetworkinfoMapper networkinfoMapper;
    @Resource
    private DisksinfoMapper disksinfoMapper;
//    @Resource
//    RedisTemplate redisTemplate;

//    @Override
//    public InfoPolymerization getNewInfo() throws JsonProcessingException {
//        OsInfoDto osInfoDto=new OsInfoDto();
//        OsRunTimeInfoDto osRunTimeInfoDto=new OsRunTimeInfoDto();
//        NetworkInfoDto networkInfoDto=new NetworkInfoDto();
//        DisksInfoDto disksInfoDto=new DisksInfoDto();
//
////        //1.拿缓存，没有再查数据库
////        ObjectMapper objectMapper = new ObjectMapper();
////        Object OsInfoCache = redisTemplate.opsForValue().get(RedisConstants.OsInfoKeysPrev);
////        Object OsruntimeInfoCache = redisTemplate.opsForValue().get(RedisConstants.OsruntimeInfoKeysPrev);
////        Object NetworkInfoCache = redisTemplate.opsForValue().get(RedisConstants.NetworkInfoKeysPrev);
////        Object DisksInfoCache = redisTemplate.opsForValue().get(RedisConstants.DisksInfoKeysPrev);
////
////
////        if(OsInfoCache!=null&&OsruntimeInfoCache!=null&&NetworkInfoCache!=null&&DisksInfoCache!=null){
////            osInfoDto=objectMapper.readValue((String)OsInfoCache, OsInfoDto.class);
////            osRunTimeInfoDto=objectMapper.readValue((String)OsruntimeInfoCache, OsRunTimeInfoDto.class);
////            networkInfoDto=objectMapper.readValue((String)NetworkInfoCache, NetworkInfoDto.class);
////            disksInfoDto=objectMapper.readValue((String)DisksInfoCache, DisksInfoDto.class);
////        }{
//
//            //2.查数据库
//            OsInfoEntity osInfoEntity=osinfoMapper.selectNew();
//            OsRunTimeInfoEntity osRunTimeInfoEntity=osruntimeinfoMapper.selectNew();
//            NetworkInfoEntity networkInfoEntity=networkinfoMapper.selectNew();
//            DisksInfoEntity disksInfoEntity=disksinfoMapper.selectNew();
//
//            BeanUtils.copyProperties(osInfoEntity,osInfoDto);
//            BeanUtils.copyProperties(osRunTimeInfoEntity,osRunTimeInfoDto);
//            BeanUtils.copyProperties(networkInfoEntity,networkInfoDto);
//            BeanUtils.copyProperties(disksInfoEntity,disksInfoDto);
//
////        }
//
//
//
//        return InfoPolymerization.builder()
//                .osInfoDto(osInfoDto)
//                .osRunTimeInfoDto(osRunTimeInfoDto)
//                .networkInfoDto(networkInfoDto)
//                .disksInfoDto(disksInfoDto)
//                .build();
//    }



    @Override
    public InfoPolymerization getInfoBetween(Long osId,LocalDateTime startTime, LocalDateTime endTime) {


        System.out.println("开始查询");
        List<OsInfoEntity> osInfoEntityList=osinfoMapper.selectBetween(osId,startTime,endTime);
        List<OsRunTimeInfoEntity> osRunTimeInfoEntityList=osruntimeinfoMapper.selectBetween(osId,startTime,endTime);
        List<NetworkInfoEntity> networkInfoEntityList=networkinfoMapper.selectBetween(osId,startTime,endTime);

        String name = networkInfoEntityList.get(0).getNetworkName();//默认只拿读到的第一个盘的
        networkInfoEntityList = networkInfoEntityList.stream().filter(networkInfoEntity -> {
           return networkInfoEntity.getNetworkName().equals(name);
        }).collect(Collectors.toList());


        InfoPolymerization res = new InfoPolymerization();
        List<Object[]> data=new ArrayList<>();
        for(int i=0;i<networkInfoEntityList.size();i++){
            Object[] objects = new Object[12];
            objects[0]=new DateTime(osRunTimeInfoEntityList.get(i).getCreateTime()).toString();
            objects[1]=osInfoEntityList.get(i).getOs();
            objects[2]=osInfoEntityList.get(i).getOsArch();
            objects[3]=osInfoEntityList.get(i).getHostName();
            objects[4]=osRunTimeInfoEntityList.get(i).getCpuUsage();
            objects[5]=osRunTimeInfoEntityList.get(i).getCpuMaxfreq();
            objects[6]=osRunTimeInfoEntityList.get(i).getCpuCurrentfreq();
            objects[7]=(double)osRunTimeInfoEntityList.get(i).getUseMemory()/osRunTimeInfoEntityList.get(i).getTotalMemory();
            objects[8]=osRunTimeInfoEntityList.get(i).getDiskReadRate();
            objects[9]=osRunTimeInfoEntityList.get(i).getDiskWriteRate();
            objects[10]=networkInfoEntityList.get(i).getSend();
            objects[11]=networkInfoEntityList.get(i).getAccept();
            data.add(objects);
        }
        res.setObjects(data);
        return res;
    }
}













//@Service
//@Slf4j
//public class IGetInfoServiceImpl implements IGetInfoService {
//
//    @Resource
//    private OsinfoMapper osinfoMapper;
//    @Resource
//    private OsruntimeinfoMapper osruntimeinfoMapper;
//    @Resource
//    private NetworkinfoMapper networkinfoMapper;
//    @Resource
//    private DisksinfoMapper disksinfoMapper;
////    @Resource
////    RedisTemplate redisTemplate;
//
////    @Override
////    public InfoPolymerization getNewInfo() throws JsonProcessingException {
////        OsInfoDto osInfoDto=new OsInfoDto();
////        OsRunTimeInfoDto osRunTimeInfoDto=new OsRunTimeInfoDto();
////        NetworkInfoDto networkInfoDto=new NetworkInfoDto();
////        DisksInfoDto disksInfoDto=new DisksInfoDto();
////
//////        //1.拿缓存，没有再查数据库
//////        ObjectMapper objectMapper = new ObjectMapper();
//////        Object OsInfoCache = redisTemplate.opsForValue().get(RedisConstants.OsInfoKeysPrev);
//////        Object OsruntimeInfoCache = redisTemplate.opsForValue().get(RedisConstants.OsruntimeInfoKeysPrev);
//////        Object NetworkInfoCache = redisTemplate.opsForValue().get(RedisConstants.NetworkInfoKeysPrev);
//////        Object DisksInfoCache = redisTemplate.opsForValue().get(RedisConstants.DisksInfoKeysPrev);
//////
//////
//////        if(OsInfoCache!=null&&OsruntimeInfoCache!=null&&NetworkInfoCache!=null&&DisksInfoCache!=null){
//////            osInfoDto=objectMapper.readValue((String)OsInfoCache, OsInfoDto.class);
//////            osRunTimeInfoDto=objectMapper.readValue((String)OsruntimeInfoCache, OsRunTimeInfoDto.class);
//////            networkInfoDto=objectMapper.readValue((String)NetworkInfoCache, NetworkInfoDto.class);
//////            disksInfoDto=objectMapper.readValue((String)DisksInfoCache, DisksInfoDto.class);
//////        }{
////
////            //2.查数据库
////            OsInfoEntity osInfoEntity=osinfoMapper.selectNew();
////            OsRunTimeInfoEntity osRunTimeInfoEntity=osruntimeinfoMapper.selectNew();
////            NetworkInfoEntity networkInfoEntity=networkinfoMapper.selectNew();
////            DisksInfoEntity disksInfoEntity=disksinfoMapper.selectNew();
////
////            BeanUtils.copyProperties(osInfoEntity,osInfoDto);
////            BeanUtils.copyProperties(osRunTimeInfoEntity,osRunTimeInfoDto);
////            BeanUtils.copyProperties(networkInfoEntity,networkInfoDto);
////            BeanUtils.copyProperties(disksInfoEntity,disksInfoDto);
////
//////        }
////
////
////
////        return InfoPolymerization.builder()
////                .osInfoDto(osInfoDto)
////                .osRunTimeInfoDto(osRunTimeInfoDto)
////                .networkInfoDto(networkInfoDto)
////                .disksInfoDto(disksInfoDto)
////                .build();
////    }
//
//
//
//    @Override
//    public InfoPolymerization getInfoBetween(Long osId,LocalDateTime startTime, LocalDateTime endTime) {
//
//
//        System.out.println("开始查询");
//        List<OsInfoEntity> osInfoEntityList=osinfoMapper.selectBetween(osId,startTime,endTime);
//        List<OsRunTimeInfoEntity> osRunTimeInfoEntityList=osruntimeinfoMapper.selectBetween(osId,startTime,endTime);
//        List<NetworkInfoEntity> networkInfoEntityList=networkinfoMapper.selectBetween(osId,startTime,endTime);
//        List<DisksInfoEntity> disksInfoEntityList=disksinfoMapper.selectBetween(osId,startTime,endTime);
//
//        System.out.println("查询完成");
//        List<Object[]> osinfoList=osInfoEntityList.stream().map(osInfoEntity -> {
//            Object[] objects=new Object[4];
//            objects[0]=new DateTime(osInfoEntity.getCreateTime()).toString();
//            objects[1]=osInfoEntity.getOs();
//            objects[2]=osInfoEntity.getOsArch();
//            objects[3]=osInfoEntity.getHostName();
//            return objects;
//        }).collect(Collectors.toList());
//
//        List<Object[]> osruntimeinfoList=osRunTimeInfoEntityList.stream().map(OsRunTimeInfoEntity -> {
//            Object[] objects=new Object[7];
//            objects[0]=new DateTime(OsRunTimeInfoEntity.getCreateTime()).toString();
//            objects[1]=OsRunTimeInfoEntity.getCpuUsage();
//            objects[2]=OsRunTimeInfoEntity.getCpuMaxfreq();
//            objects[3]=OsRunTimeInfoEntity.getCpuCurrentfreq();
//            objects[4]=(double)OsRunTimeInfoEntity.getUseMemory()/OsRunTimeInfoEntity.getTotalMemory();
//            objects[5]=OsRunTimeInfoEntity.getDiskReadRate();
//            objects[6]=OsRunTimeInfoEntity.getDiskWriteRate();
//            return objects;
//
//        }).collect(Collectors.toList());
//
//
//        List<Object[]> networkinfoList = networkInfoEntityList.stream().map(networkInfoEntity -> {
//            Object[] objects = new Object[3];
//            objects[0]=new DateTime(networkInfoEntity.getCreateTime()).toString();
//            objects[1]=networkInfoEntity.getSend();
//            objects[2]=networkInfoEntity.getAccept();
//            return objects;
//        }).collect(Collectors.toList());
//
//        InfoPolymerization res = new InfoPolymerization();
//        for (Object[] objects : osinfoList) {
//            res.getOsInfoDto().add(objects);
//        }
//
//        for (Object[] objects : osruntimeinfoList) {
//            res.getOsRunTimeInfoDto().add(objects);
//        }
//        for (Object[] objects : networkinfoList) {
//            res.getNetworkInfoDto().add(objects);
//        }
//
//        return res;
//    }
//}


//
//
//        List<OsInfoDto> osInfoDtoList = osInfoEntityList.stream().map(osInfoEntity -> {
//            OsInfoDto osInfoDto = new OsInfoDto();
//            BeanUtils.copyProperties(osInfoEntity, osInfoDto);
//            return osInfoDto;
//        }).collect(Collectors.toList());
//        List<OsRunTimeInfoDto>  osRunTimeInfoDtoList= osRunTimeInfoEntityList.stream().map(OsRunTimeInfoEntity -> {
//            OsRunTimeInfoDto osRunTimeInfoDto = new OsRunTimeInfoDto();
//            BeanUtils.copyProperties(OsRunTimeInfoEntity,osRunTimeInfoDto );
//            return osRunTimeInfoDto;
//        }).collect(Collectors.toList());
//
//        List<NetworkInfoDto> networkInfoDtoList = networkInfoEntityList.stream().map(networkInfoEntity -> {
//            NetworkInfoDto networkInfoDto = new NetworkInfoDto();
//            BeanUtils.copyProperties(networkInfoEntity, networkInfoDto);
//            return networkInfoDto;
//        }).collect(Collectors.toList());
//
//        List<DisksInfoDto> disksInfoDtoList = disksInfoEntityList.stream().map(disksInfoEntity -> {
//            DisksInfoDto disksInfoDto = new DisksInfoDto();
//            BeanUtils.copyProperties(disksInfoEntity, disksInfoDto);
//            return disksInfoDto;
//        }).collect(Collectors.toList());
//
//        List<InfoPolymerization> res = new ArrayList<>();
//        for(int i=0;i<osInfoDtoList.size();i++){
//            res.add(InfoPolymerization.builder()
//                    .osInfoDto(osInfoDtoList.get(i))
//                    .osRunTimeInfoDto(osRunTimeInfoDtoList.get(i))
//                    .networkInfoDto(networkInfoDtoList.get(i))
//                    .disksInfoDto(disksInfoDtoList.get(i))
//                    .build());
//        }
//        return res;